// utils/eventBus.js
class EventBus {
  constructor() {
    this.events = {}; // 存储事件及回调
  }

  on(eventName, callback) {
    if (!this.events[eventName]) {
      this.events[eventName] = [];
    }
    this.events[eventName].push(callback);
  }

  emit(eventName, ...args) {
    const callbacks = this.events[eventName] || [];
    callbacks.forEach(cb => cb(...args));
  }

  off(eventName, callback) {
    if (this.events[eventName]) {
      this.events[eventName] = callback 
        ? this.events[eventName].filter(cb => cb !== callback)
        : [];
    }
  }
}

export const eventBus = new EventBus(); // 创建全局实例